package com.itqf.realm;

import com.itqf.bean.DtsAdmin;
import com.itqf.service.DtsAdminService;
import com.itqf.service.DtsPermissionService;
import com.itqf.service.DtsRoleService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;

/**
 * projectName: shop_manage
 *
 * @author:
 * time: 2021/7/12 17:53
 * description:
 */
public class AuthorizationRealm extends AuthorizingRealm {
    public static final Logger logger = LoggerFactory.getLogger(AuthorizationRealm.class);

    @Autowired
    public DtsAdminService dtsAdminService;
    @Autowired
    public DtsRoleService dtsRoleService;
    @Autowired
    public DtsPermissionService dtsPermissionService;

    //MD5加盐
    public static final String SLAT = "store";
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //1·获取用户权限
        DtsAdmin dtsAdmin = (DtsAdmin) principalCollection.getPrimaryPrincipal();

        if (dtsAdmin == null) {
            logger.warn("[realm授权]用户信息为null 无法查询授权数据");
            return null;
        }
        //2·查询权限结合
        Set<String>permissionNames = dtsPermissionService.queryPermissionName(dtsAdmin.getRoleIds());
        //3·查询角色集合
        List<String>roleName = dtsRoleService.queryRoleNames(dtsAdmin.getRoleIds());
        logger.debug("[realm授权]查询角色的权限为：{}，查询角色为：{}",permissionNames,roleName);
        //4·封装数据返回
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addStringPermissions(permissionNames);
        authorizationInfo.addRoles(roleName);
        return authorizationInfo;
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1·获取账号信息
        String username = (String) authenticationToken.getPrincipal();
        logger.debug("[realm认证]用户输入的账号为null");

        if (username == null) {
            logger.warn("[realm认证]用户名不能为空");
            return null;
        }
        //2·根据账号查询数据库
        DtsAdmin admin = dtsAdminService.queryByUsername(username);
        logger.debug("[realm认证]根据username：{},查询的用户对象为：{}",username,admin);

        //3·判断账号返回结果
        if (admin == null) {
            logger.warn("[realm认证]用户名没有查询到相应的用户数据");
        }
        //4·账号非空封装对象
        SimpleAuthenticationInfo
        info = new SimpleAuthenticationInfo
        (admin,admin.getPassword(), ByteSource.Util.bytes(SLAT),"myrealm");
        //5·返回认证数据对象
        return info;
    }
}
